<template>
  <div class="data-source-container">
    <div class="toolbar">
      <el-button type="primary" @click="handleCreateDataSource">
        <el-icon><Plus /></el-icon>新建数据源
      </el-button>
      <el-input
        v-model="searchQuery"
        placeholder="搜索数据源名称"
        class="search-input"
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
    </div>

    <el-table :data="dataSourceList" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="数据源名称" />
      <el-table-column prop="type" label="类型">
        <template #default="{ row }">
          <el-tag>{{ row.type }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="host" label="主机地址" />
      <el-table-column prop="port" label="端口" width="100" />
      <el-table-column prop="status" label="连接状态">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? "已连接" : "未连接" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button-group>
            <el-button type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button
              type="primary"
              link
              @click="handleTest(row)"
              :loading="row.testing"
            >
              测试连接
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Plus, Search } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";

const loading = ref(false);
const searchQuery = ref("");
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

const dataSourceList = ref([
  {
    id: 1,
    name: "主数据库",
    type: "MySQL",
    host: "localhost",
    port: 3306,
    status: 1,
    testing: false,
  },
  {
    id: 2,
    name: "Redis缓存",
    type: "Redis",
    host: "redis.example.com",
    port: 6379,
    status: 1,
    testing: false,
  },
  {
    id: 3,
    name: "MongoDB文档库",
    type: "MongoDB",
    host: "mongo.example.com",
    port: 27017,
    status: 0,
    testing: false,
  },
]);

const handleCreateDataSource = () => {
  // TODO: 实现创建数据源逻辑
};

const handleEdit = (row: any) => {
  // TODO: 实现编辑数据源逻辑
};

const handleTest = async (row: any) => {
  row.testing = true;
  try {
    // TODO: 实现测试连接逻辑
    await new Promise((resolve) => setTimeout(resolve, 1000));
    ElMessage.success("连接测试成功");
  } catch (error) {
    ElMessage.error("连接测试失败");
  } finally {
    row.testing = false;
  }
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm("确定要删除该数据源吗？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    // TODO: 实现删除数据源逻辑
    ElMessage.success("删除成功");
  });
};

const handleSizeChange = (val: number) => {
  pageSize.value = val;
  // TODO: 重新加载数据
};

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  // TODO: 重新加载数据
};
</script>

<style scoped>
.data-source-container {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}

.search-input {
  width: 300px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 